package com.lanou.liuqixin.datebasedemo.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.liuqixin.datebasedemo.R;

import java.util.List;

/**
 * Created by dllo on 17/4/19.
 */

public class MainFragmentAdapter extends FragmentPagerAdapter {
    //装载每一个标签的title的数组
    private String[] titles = new String[]{
            "油耗", "排名", "油费", "费用"
    };
    //装载每一个标签的img的数组
    private int[] imgs = new int[]{
            R.mipmap.youhao,
            R.mipmap.paiming,
            R.mipmap.youfei,
            R.mipmap.feiyong
    };
    //声明一个list集合 用来接收数据
    private List<Fragment> mfragments;

    public MainFragmentAdapter(FragmentManager fm, List<Fragment> mfragments) {
        super(fm);
        this.mfragments = mfragments;

    }

    @Override
    public Fragment getItem(int position) {
        return mfragments.get(position);
    }

    @Override
    public int getCount() {
        return mfragments.size();
    }

    public View getTabView(int index, Context context) {
        //通过LayoutInflater类的静态方法from
        //得到一个LayoutInflater对象
        LayoutInflater inflater = LayoutInflater.from(context);
        //通过inflater对象的inflate方法
        //将布局xml文件,注入成一个view对象
        View tabView = inflater.inflate(R.layout.activity_tab_view, null);
        //通过tabView对象,找到视图中的textView与ImageView
        TextView titleTv = (TextView) tabView.findViewById(R.id.tab_tv);
        ImageView imageView = (ImageView) tabView.findViewById(R.id.tab_iv);
        //设置标题文字和图片
        titleTv.setText(titles[index]);
        imageView.setImageResource(imgs[index]);
        return tabView;
    }


}
